//
//  RegisterView.swift
//  XMGG
//
//  Created by iDev on 7/1/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit

class RegisterView: UIView, UITextFieldDelegate {

    private var whiteView = UIView()
    
    private var phoneLable:UILabel!
    private var phoneInput:UITextField!
    
    private var vertifyLable:UILabel!
    private var vertifyInput:UITextField!
    private let vertifyBtn = UIButton()
    
    private var userName:UILabel!
    private var nameInput:UITextField!
    
    private var userPwd:UILabel!
    private var pwdInput:UITextField!
    
    private let registerBtn = UIButton()
    private let backBtn = UIButton()
    
    private var line0:UIView!
    private var line1:UIView!
    private var line2:UIView!
    private var line3:UIView!
    private var line00:UIView!
    private var line01:UIView!
    private var line02:UIView!
    private var line03:UIView!
    
    private var remainSeconds = 0
    private var timer:Timer?
//    var delegate:RegisterViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
        setupLine()
        setupLayout()
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(swipup))
        gesture.direction = .up
        self.addGestureRecognizer(gesture)
    }
    
    func setupView(){
        addSubview(whiteView)
        whiteView.backgroundColor = UIColor(white: 1.0, alpha: 0.74)
        whiteView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(170)
            make.centerX.equalToSuperview()
            make.width.equalTo(330)
            make.height.equalTo(317)
        }
        
        phoneLable = setupLabelView(text: "手机")
        whiteView.addSubview(phoneLable)
        phoneInput = setupInputView(text: "请输入手机号码")
        phoneInput.keyboardType = .numberPad
        whiteView.addSubview(phoneInput)
        
        vertifyLable = setupLabelView(text: "验证码")
        whiteView.addSubview(vertifyLable)
        vertifyInput = setupInputView(text: "请输入验证码")
        vertifyInput.keyboardType = .numberPad
        whiteView.addSubview(vertifyInput)
        vertifyBtn.setBackgroundImage(colorImg(color: UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1.0)), for: .normal)
        vertifyBtn.setTitle("获取验证码", for: .normal)
        vertifyBtn.titleLabel?.font = UIFont(name: "PingFang SC", size: 16*0.75)
        vertifyBtn.layer.cornerRadius = 9
        vertifyBtn.layer.masksToBounds = true
        vertifyBtn.addTarget(RegisterViewController.self(), action: NSSelectorFromString("verifyCodeReq"), for: .touchUpInside)
        whiteView.addSubview(vertifyBtn)
        
        userName = setupLabelView(text: "姓名")
        whiteView.addSubview(userName)
        nameInput = setupInputView(text: "请输入姓名")
        whiteView.addSubview(nameInput)
        
        userPwd = setupLabelView(text: "密码")
        whiteView.addSubview(userPwd)
        pwdInput = setupInputView(text: "请输入密码")
        pwdInput.isSecureTextEntry = true
        whiteView.addSubview(pwdInput)
        
        whiteView.addSubview(registerBtn)
        registerBtn.setBackgroundImage(colorImg(color: UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1.0)), for: .normal)
        registerBtn.setTitle("注  册", for: .normal)
        registerBtn.layer.cornerRadius = 3
        registerBtn.layer.masksToBounds = true
        registerBtn.addTarget(RegisterViewController.self(), action: NSSelectorFromString("userRequestRegister"), for: .touchUpInside)
        
        addSubview(backBtn)
        backBtn.setBackgroundImage(UIImage(named: "content_btnclose"), for: .normal)
        backBtn.layer.cornerRadius = 15
        backBtn.layer.masksToBounds = true
        backBtn.addTarget(RegisterViewController.self(), action: NSSelectorFromString("backBtnClick"), for: .touchUpInside)
    }

    func setupLayout(){
        backBtn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(112.5)
            make.right.equalToSuperview().offset(-22.5)
            make.width.height.equalTo(30)
        }
        
        phoneLable.snp.makeConstraints { (make) in
            make.centerY.equalTo(line00)
            make.centerX.equalTo(line0.snp.left).offset(32)
        }
        phoneInput.snp.makeConstraints { (make) in
            make.centerY.equalTo(line00)
            make.left.equalTo(line00.snp.left).offset(11)
            make.right.equalToSuperview().offset(-46)
        }
        vertifyLable.snp.makeConstraints { (make) in
            make.centerY.equalTo(line01)
            make.centerX.equalTo(line1.snp.left).offset(32)
        }
        vertifyBtn.snp.makeConstraints { (make) in
            make.right.equalTo(line1)
            make.centerY.equalTo(line01)
            make.width.equalTo(71.5)
            make.height.equalTo(24.5)
        }
        vertifyInput.snp.makeConstraints { (make) in
            make.centerY.equalTo(line01)
            make.left.equalTo(line01.snp.left).offset(11)
            make.right.equalTo(vertifyBtn.snp.left)
        }
        
        userName.snp.makeConstraints { (make) in
            make.centerY.equalTo(line02)
            make.centerX.equalTo(line2.snp.left).offset(32)
        }
        nameInput.snp.makeConstraints { (make) in
            make.centerY.equalTo(line02)
            make.left.equalTo(line02.snp.left).offset(11)
            make.right.equalToSuperview().offset(-46)
        }
        userPwd.snp.makeConstraints { (make) in
            make.centerY.equalTo(line03)
            make.centerX.equalTo(line3.snp.left).offset(32)
        }
        pwdInput.snp.makeConstraints { (make) in
            make.centerY.equalTo(line03)
            make.left.equalTo(line03.snp.left).offset(11)
            make.right.equalToSuperview().offset(-46)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(line3)
            make.height.equalTo(33)
            make.top.equalTo(line3).offset(46)
        }
    }
    
    func setupLabelView(text:String)->UILabel{
        let lable = UILabel()
        lable.text = text
        lable.font = UIFont(name: "PingFang SC", size: 23*0.75)
        lable.textColor = UIColor(red: 17/255.0, green: 17/255.0, blue: 17/255.0, alpha: 1)
        return lable
    }
    
    func setupInputView(text:String)->UITextField{
        let input = UITextField()
        input.delegate = self
        input.attributedPlaceholder = NSAttributedString(string: text, attributes: [NSForegroundColorAttributeName:UIColor(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1.0),NSFontAttributeName:UIFont(name: "PingFang SC", size: 23*0.75)!])
        input.font = UIFont(name: "PingFang SC", size: 23*0.75)
        return input
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        firstRespondView = textField
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        firstRespondView?.resignFirstResponder()
        return true
    }
    
    func swipup(){
        firstRespondView?.resignFirstResponder()
    }
    
    func getViewModle()->RegisterModle{
        let modle = RegisterModle(tu_account: phoneInput.text, tu_password: pwdInput.text, tu_name: nameInput.text, tu_tel: phoneInput.text, tu_email: phoneInput.text, tu_genDate: getCurrentTime(), tu_description: "smjbwy", tu_verfcode: vertifyInput.text)
        return modle
    }
    
    func getPhone()->String{
        return phoneInput.text ?? ""
    }
    
    func verifyBtnDisable(){
        remainSeconds = 10
        vertifyBtn.isEnabled = false
        timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (time) in
            self.remainSeconds -= 1;
            if self.remainSeconds < 0 {
                time.invalidate()
                self.vertifyBtn.isEnabled = true
                self.vertifyBtn.setTitle("获取验证码", for: .normal)
            }else{
                self.vertifyBtn.setTitle("(\(self.remainSeconds))", for: .disabled)
                
            }
        })
    }
  
    private func makeLine()->UIView {
        let sepreateLine = UIView()
        sepreateLine.backgroundColor = UIColor(red: 201/255.0, green: 201/255.0, blue: 201/255.0, alpha: 1.0)
        return sepreateLine
    }
    
    func setupLine(){
        line0 = makeLine()
        whiteView.addSubview(line0)
        line0.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(24)
            make.top.equalToSuperview().offset(63)
            make.right.equalToSuperview().offset(-24)
            make.height.equalTo(1)
        }
        line1 = makeLine()
        whiteView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(line0)
            make.top.equalTo(line0).offset(46)
            make.right.equalTo(line0)
            make.height.equalTo(1)
        }
        line2 = makeLine()
        whiteView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line0)
            make.top.equalTo(line1).offset(46)
            make.right.equalTo(line0)
            make.height.equalTo(1)
        }
        line3 = makeLine()
        whiteView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line0)
            make.top.equalTo(line2).offset(46)
            make.right.equalTo(line0)
            make.height.equalTo(1)
        }
        
        line00 = makeLine()
        whiteView.addSubview(line00)
        line00.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.bottom.equalTo(line0).offset(-2.5)
            make.height.equalTo(28)
            make.left.equalTo(line0).offset(64)
        }
        line01 = makeLine()
        whiteView.addSubview(line01)
        line01.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.height.equalTo(line00)
            make.left.equalTo(line00)
            make.bottom.equalTo(line1).offset(-2.5)
        }
        line02 = makeLine()
        whiteView.addSubview(line02)
        line02.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.height.equalTo(line00)
            make.left.equalTo(line00)
            make.bottom.equalTo(line2).offset(-2.5)
        }
        line03 = makeLine()
        whiteView.addSubview(line03)
        line03.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.height.equalTo(line00)
            make.left.equalTo(line00)
            make.bottom.equalTo(line3).offset(-2.5)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
